<template>
  <view class="container">
    <FilterNavbar :searchList="searchList" v-model:queryParam="queryParam" @confirm="onFilterNavbarConfirm">
      <template #center>
        <view class="navbar-title"> 违章短信记录 </view>
      </template>
    </FilterNavbar>
    <view class="manager-search">
      <ve-search-bar :showBorder="true" placeholder="请输入车牌号/承租人姓名/违章单号" @search="searchBarSearch" />
    </view>
    <scroll-view
      scroll-y
      :scroll-top="scrollTop"
      :scroll-with-animation="true"
      refresher-enabled
      :refresher-triggered="refreshStatus"
      :enable-back-to-top="true"
      @refresherrefresh="resetRefresh(true)"
      @scrolltolower="reachBottom"
    >
      <view class="record-list">
        <block v-for="item in dataSource" :key="item">
          <SmsItem :record="item" :resetRefresh="resetRefresh" />
        </block>
      </view>
      <!-- 加载结束列表为空时展示 -->
      <ve-empty v-if="dataSource.length == 0 && loadStatus == 'nomore'"></ve-empty>
      <!-- 底部加载状态 -->
      <ve-load-more v-else :status="loadStatus" bgColor="#F7F7F9"></ve-load-more>
      <!-- loading组件 -->
      <ve-toast ref="toastRef" />
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
  import FilterNavbar from '@/components/FilterNavbar'
  import SmsItem from './components/sms-item.vue'
  import { computed, ref } from 'vue'
  import { usePageDict } from '@/hooks/useDict'
  import useList from '@/hooks/useList'
  import { onLoad } from '@dcloudio/uni-app'

  const queryParam = ref<any>({
    smsKeyword: undefined,
    pageNumber: 1,
  })

  const { resetRefresh, reachBottom, loadData, scrollTop, loadStatus, refreshStatus, dataSource } = useList(
    '/V5/violation/getSmsList',
    queryParam,
  )

  const dict = usePageDict('carViolationDict')
  const searchList = computed(() => {
    return [
      {
        prop: ['eventTimeStart', 'eventTimeEnd'],
        label: '违章时间',
        type: 'daterange-picker',
        attrs: {
          mode: 'date',
        },
      },
      {
        prop: 'eventStatusList',
        label: '违章状态',
        type: 'remote-select',
        attrs: {
          type: 'dict',
          title: '选择违章状态',
          multiple: true, // 多选
          outData: dict.ViolationEventStatusEnum,
        },
      },
      {
        prop: 'memberPhone',
        label: '承租人电话',
      },
    ]
  })

  const searchBarSearch = (val: any) => {
    queryParam.value.smsKeyword = val
    resetRefresh()
  }

  const onFilterNavbarConfirm = (params: any) => {
    queryParam.value = {
      ...queryParam.value,
      ...params,
    }
    resetRefresh()
  }

  onLoad(() => {
    loadData()
  })
</script>

<style lang="less" scoped>
  .container {
    height: 100vh;
    background-color: rgba(247, 248, 250, 1);
  }
  .navbar-title {
    font-size: 34rpx;
    font-weight: 500;
    color: #1d2129;
  }
  .record-list {
    padding: 24rpx 24rpx;
  }
  .manager-search {
    position: sticky;
    top: 44px;
  }
</style>
